package com.project.poetry.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.poetry.common.util.CommonUtil;
import com.project.poetry.service.entity.po.author.AuthorPO;
import com.project.poetry.service.entity.po.poetry.PoetryPO;
import com.project.poetry.service.mapper.AuthorMapper;
import com.project.poetry.service.service.CodeService;
import com.project.poetry.service.service.PoetryService;
import com.project.poetry.service.service.WebService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.*;

/**
 * web业务层实现类
 * @author mengxj
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WebServiceImpl implements WebService {

    @Resource
    private AuthorMapper authorMapper;

    @Resource
    private CodeService codeService;

    @Resource
    private PoetryService poetryService;

    @Override
    public ModelAndView savePoetryPage(Long poetryId) {
        List<AuthorPO> authorList = this.authorMapper.selectList(new QueryWrapper<>());
        Map<String, Object> res = new HashMap<>(2);
        res.put("authorList", authorList);
        res.put("codes", this.codeService.getCodesByType(20));
        PoetryPO poetry;
        String idParam = "";
        if(Objects.nonNull(poetryId)){
            idParam = String.format("id: %s,", poetryId);
            poetry = this.poetryService.getById(poetryId);
            poetry.setContent(CommonUtil.convertJsonToString(poetry.getContent()));
        }else{
            poetry = new PoetryPO();
        }
        res.put("idParam", idParam);
        res.put("poetry", poetry);
        return CommonUtil.success("service/poetry/savePoetry.html", res);
    }
}
